<?php

/**
 *      This is NOT a freeware, use is subject to license terms
 *      应用名称: DiscuzQ 转 DiscuzX 1.2
 *      下载地址: https://addon.dismall.com/plugins/qconverx.html
 *      应用开发者: 科站网discuzlab.com
 *      开发者QQ: 839515338
 *      更新日期: 202312061210
 *      授权域名: 106.55.100.165
 *      授权码: 2023120604xgi69i0r51
 *      未经应用程序开发者/所有者的书面许可，不得进行反向工程、反向汇编、反向编译等，不得擅自复制、修改、链接、转载、汇编、发表、出版、发展与之有关的衍生产品、作品等
 */

if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
function downLoadRemoteAttach($attach){
	global $_G;
	if(!$attach['is_remote']) return 0;
	$cosClient = new Qcloud\Cos\Client(
		array(
			'region' => $_G['cache']['qconverx']['qcloud_cos_bucket_area'],
			'schema' => 'https',
			'credentials' => array(
				'secretId'  => $_G['cache']['qconverx']['qcloud_secret_id'],
				'secretKey' => $_G['cache']['qconverx']['qcloud_secret_key']
			)
		)
	);
	try {
		$bucket = $_G['cache']['qconverx']['qcloud_cos_bucket_name']; 
		$key = $attach['file_path'] . $attach['attachment'];
		$signedUrl = $cosClient->getObjectUrl($bucket, $key, '+20 minutes');
		// 请求成功
		$path = DISCUZ_ROOT.'./'.str_replace('public/attachments/', '', $attach['file_path']);
		dmkdir($path);
		file_put_contents($path . $attach['attachment'], fopen($signedUrl, 'r'));
		if(filesize($path . $attach['attachment']) == $attach['file_size']){
			return 0;
		}else{
			@unlink($path . $attach['attachment']);
			return 1;
		}
	} catch (\Exception $e) {
		return 1;
	}
}